package p3;

public class CPUDevice implements DeviceInterface {

	/** A queue for processes waiting to use the cpu  **/
	private Queue cpuQueue;
	
	/** A reference to the statistics collector */
	private Statistics statistics;
	
	/** The current running process */
	private Process currentProcess;
	
	
	public CPUDevice(Queue cpuQueue, Statistics statistics)
	{
		this.cpuQueue = cpuQueue;
		this.statistics = statistics;
	}

	@Override
	public void insertProcess(Process p) {
		cpuQueue.insert(p);
	}

	@Override
	public void timePassed(long timePassed) {
		statistics.cpuQueueLengthTime += cpuQueue.getQueueLength()*timePassed;
		if (cpuQueue.getQueueLength() > statistics.cpuQueueLargestLength) {
			statistics.cpuQueueLargestLength = cpuQueue.getQueueLength(); 
		}
	}


	@Override
	public Process popNextProcess() {
//		Process p = (Process) cpuQueue.getNext();
		if (cpuQueue.getQueueLength() == 0) return null;
		return (Process) cpuQueue.removeNext();
	}
	
	public Process getCurrentProcess(){
		return currentProcess;
	}
	
	public void setCurrentProcess(Process p){
		currentProcess = p;
	}
}